package com.cnhis.cloudhealth.clinical.util;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.sun.swing.internal.plaf.synth.resources.synth;

public class PiplineUtil {

	public static String preadd_ini_var = iniPool_setPoolLimitNpreadd();

	synchronized private static String iniPool_setPoolLimitNpreadd() {

		PropertiesUtil_prjcli_V2s316 cfg=new PropertiesUtil_prjcli_V2s316(PropertiesUtil_prjcli_V2s316.class.getResourceAsStream("/META-INF/prop/cache.properties"));
		Object config = cfg.getConfig("lev1_thrdpool_size");
		 
		//
		int lev1_pool_count = Integer.parseInt(config.toString().trim());
		int lev2_pool_count =  cfg.getIntConfigWzDefval("lev2_thrdpool_size",2);
		int lev3_pool_count =  cfg.getIntConfigWzDefval("lev3_thrdpool_size",2);
		PiplineUtil.ini_pool_lev1(lev1_pool_count);
		PiplineUtil.ini_pool_lev2(lev2_pool_count);
		PiplineUtil.ini_pool_lev3(lev3_pool_count);

		return "ok";
	}

	public static ExecutorService pool_ExecutorService_hiprid_lev1  ;//= Executors.newFixedThreadPool(lev_pool_count);
	public static ExecutorService pool_ExecutorService_normal_lev2  ; //= Executors.newFixedThreadPool(1);
	public static ExecutorService pool_ExecutorService_lowprid_lev3 ;// = Executors.newFixedThreadPool(lev_pool_count);
	 
	
	public static void main(String[] args) {
		//ini_pool();
		
		 
	}

	private static void ini_pool() {
		PiplineUtil.ini_pool_lev1(30);
		PiplineUtil.ini_pool_lev2(1);
		PiplineUtil.ini_pool_lev3(1);
		System.out.println("--");
	}

public synchronized static void ini_pool_lev1(int lev_pool_count) {
	if(pool_ExecutorService_hiprid_lev1!=null)
		return;
		    
	pool_ExecutorService_hiprid_lev1=Executors.newFixedThreadPool(lev_pool_count);
		
 
		 
		 
		 for(int i=0;i<lev_pool_count;i++)
			{
				 FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

						@Override
						public Object call()   {				 
						 
							return null;
						}
					}) ;
				 pool_ExecutorService_hiprid_lev1.submit(FutureTask_updateCviBdj, "threadName");
			}
		
	}


public synchronized static void ini_pool_lev2(int lev_pool_count) {
	if(pool_ExecutorService_normal_lev2!=null)
		return;
		    
	pool_ExecutorService_normal_lev2=Executors.newFixedThreadPool(lev_pool_count);
		
 
		 
		 
		 for(int i=0;i<lev_pool_count;i++)
			{
				 FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

						@Override
						public Object call()   {				 
						 
							return null;
						}
					}) ;
				 pool_ExecutorService_normal_lev2.submit(FutureTask_updateCviBdj, "threadName");
			}
		
	}

public synchronized static void ini_pool_lev3(int lev_pool_count) {
	if(pool_ExecutorService_lowprid_lev3!=null)
		return;
		    
	pool_ExecutorService_lowprid_lev3=Executors.newFixedThreadPool(lev_pool_count);
		
 
		 
		 
		 for(int i=0;i<lev_pool_count;i++)
			{
				 FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

						@Override
						public Object call()   {				 
						 
							return null;
						}
					}) ;
				 pool_ExecutorService_lowprid_lev3.submit(FutureTask_updateCviBdj, "threadName");
			}
		
	}

public synchronized	static void ini_pool(ExecutorService es1, int lev_pool_count) {
	
	if(es1!=null)
		return;
		    
 es1=Executors.newFixedThreadPool(lev_pool_count);
		
 
		 
		 
		 for(int i=0;i<lev_pool_count;i++)
			{
				 FutureTask<Object> FutureTask_updateCviBdj=new FutureTask( new Callable<Object>() {

						@Override
						public Object call()   {				 
						 
							return null;
						}
					}) ;
				 es1.submit(FutureTask_updateCviBdj, "threadName");
			}
	}

	public static Object getRetObj(ExecutionException e) {
		Throwable cause = e.getCause();			 
		 
			RunEx re=(RunEx) cause;
			 return   re.returnObj;
			
	}
	
	
	public static Object getRetObj(ChkFailEx e) {
		Throwable cause = e.getCause();			 
		 
			RunEx re=(RunEx) cause;
			 return   re.returnObj;
			
	}


	public static boolean isEx(String string, ExecutionException e) {
		Throwable cause = e.getCause();			 
		if(cause.getMessage().contains("validity_pipline_err"))
		{
			return true;
			
		}else return false;
	}


	public static Object getRetObj_ChkFailEx(ExecutionException e) {
		Throwable cause = e.getCause();			 
		 
		ChkFailEx re=(ChkFailEx) cause;
		 return   re.returnObj;
	}

 

}
